package practice1_100;

import java.util.Stack;

public class Yuanfudao {
    public int count(String string) {
        String[] strings = string.split("");
        Stack<String> stack = new Stack<>();
        stack.push(strings[0]);
        int res = 0;
        int headBox = 0, afterBox = 0;
        for (int i = 1; i < strings.length; i++) {
            if (strings[i].equals("]")) {
                stack.pop();
                if (i + 1 < strings.length ) {
                    if(!strings[i+1].equals("[") && !strings[i+1].equals("]")){
                        headBox = Integer.parseInt(strings[i+1]);
                    }else {
                        headBox=1;
                    }
                        if(res==0){
                            res+=1*headBox;
                            afterBox=1*headBox;
                        }
                        else{
                            res+=afterBox*headBox;
                            afterBox=afterBox*headBox;
                        }
                }
            } else if (strings[i].equals("[")) stack.push(strings[i]);
        }
        return res;
    }

    public static void main(String[] args) {
        int res = new Yuanfudao().count("[[[]]2]3");
        System.out.println(res);
    }
}
